package com.ph.irp.base.verifyproject.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.ph.irp.base.accountbook.dao.PBaseAccountbookDAO;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectCandiateDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectDepartmentDao;
import com.ph.irp.base.verifyproject.service.PBaseVerifyprojectDepartmentService;
import com.ph.irp.dto.DepartmentDTO;
import com.ph.irp.model.PBaseAccountbook;
import com.ph.irp.model.PBaseVerifyprojectCandiate;
import com.ph.irp.model.PBaseVerifyprojectDepartment;

public class PBaseVerifyprojectDepartmentServiceImpl implements
		PBaseVerifyprojectDepartmentService {
	private PBaseVerifyprojectDepartmentDao departmentDao;
	private PBaseAccountbookDAO accountDao;
	private PBaseVerifyprojectCandiateDao candiateDao;
	
	public void setDepartmentDao(PBaseVerifyprojectDepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}
	
	public void setAccountDao(PBaseAccountbookDAO accountDao) {
		this.accountDao = accountDao;
	}
	
	public void setCandiateDao(PBaseVerifyprojectCandiateDao candiateDao) {
		this.candiateDao = candiateDao;
	}

	@Override
	public void addDepartment(PBaseVerifyprojectDepartment department) throws Throwable
			{
		departmentDao.addDepartment(department);
	}

	@Override
	public void delateDepartment(int departmentId) throws Throwable {
		departmentDao.delateDepartment(departmentId);
	}

	@Override
	public List<DepartmentDTO> getAllDepartment() throws Throwable{
			List<DepartmentDTO> departments = new ArrayList<DepartmentDTO>();
			List<PBaseVerifyprojectDepartment> list = departmentDao.getAllDepartment();
			for(PBaseVerifyprojectDepartment pbdv : list){
				DepartmentDTO depart = new DepartmentDTO();
				int PBaseVerifyprojectCandiateId = pbdv.getPBaseVerifyprojectCandiateId();
				int PBaseAccountbookId = pbdv.getPBaseAccountbookId();		
				depart.setDepartment(pbdv);
				if(PBaseVerifyprojectCandiateId != 0){
					PBaseVerifyprojectCandiate manager = candiateDao.getCandiateById(PBaseVerifyprojectCandiateId);
					depart.setManager(manager);
				}
				if(PBaseAccountbookId != 0){
					PBaseAccountbook account = accountDao.getAccountbookById(PBaseAccountbookId);
					depart.setAccountbook(account);
				}
				departments.add(depart);
			}
			return departments;
	}

	@Override
	public DepartmentDTO getDepartmentById(int departmentId) throws Throwable{
			DepartmentDTO depart = new DepartmentDTO();
			PBaseVerifyprojectDepartment department = departmentDao.getDepartmentById(Integer.valueOf(departmentId));
			
			int PBaseVerifyprojectCandiateId = department.getPBaseVerifyprojectCandiateId();
			int PBaseAccountbookId = department.getPBaseAccountbookId();
			
			depart.setDepartment(department);
			if(PBaseVerifyprojectCandiateId != 0){
				PBaseVerifyprojectCandiate manager = candiateDao.getCandiateById(PBaseVerifyprojectCandiateId);
				depart.setManager(manager);
			}
			if(PBaseAccountbookId != 0){
				PBaseAccountbook account = accountDao.getAccountbookById(PBaseAccountbookId);
				depart.setAccountbook(account);
			}
			return depart;
	}

	@Override
	public void updateDepartment(PBaseVerifyprojectDepartment department) throws Throwable{
		departmentDao.updateDepartment(department);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DepartmentDTO> getDepartmentByCode(String code)
			throws Throwable {
		List<DepartmentDTO> departments = new ArrayList<DepartmentDTO>();
		List<PBaseVerifyprojectDepartment> list = departmentDao.getDepartmentByCode(code);
		for(PBaseVerifyprojectDepartment pbdv : list){
			DepartmentDTO depart = new DepartmentDTO();
			int PBaseVerifyprojectCandiateId = pbdv.getPBaseVerifyprojectCandiateId();
			int PBaseAccountbookId = pbdv.getPBaseAccountbookId();		
			depart.setDepartment(pbdv);
			if(PBaseVerifyprojectCandiateId != 0){
				PBaseVerifyprojectCandiate manager = candiateDao.getCandiateById(PBaseVerifyprojectCandiateId);
				depart.setManager(manager);
			}
			if(PBaseAccountbookId != 0){
				PBaseAccountbook account = accountDao.getAccountbookById(PBaseAccountbookId);
				depart.setAccountbook(account);
			}
			departments.add(depart);
		}
		return departments;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DepartmentDTO> getDepartmentByCodeAndName(String code,
			String name) throws Throwable {
		List<DepartmentDTO> departments = new ArrayList<DepartmentDTO>();
		List<PBaseVerifyprojectDepartment> list = departmentDao.getDepartmentByCodeAndName(code, name);
		for(PBaseVerifyprojectDepartment pbdv : list){
			DepartmentDTO depart = new DepartmentDTO();
			int PBaseVerifyprojectCandiateId = pbdv.getPBaseVerifyprojectCandiateId();
			int PBaseAccountbookId = pbdv.getPBaseAccountbookId();		
			depart.setDepartment(pbdv);
			if(PBaseVerifyprojectCandiateId != 0){
				PBaseVerifyprojectCandiate manager = candiateDao.getCandiateById(PBaseVerifyprojectCandiateId);
				depart.setManager(manager);
			}
			if(PBaseAccountbookId != 0){
				PBaseAccountbook account = accountDao.getAccountbookById(PBaseAccountbookId);
				depart.setAccountbook(account);
			}
			departments.add(depart);
		}
		return departments;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DepartmentDTO> getDepartmentByName(String name)
			throws Throwable {
		List<DepartmentDTO> departments = new ArrayList<DepartmentDTO>();
		List<PBaseVerifyprojectDepartment> list = departmentDao.getDepartmentByName(name);
		for(PBaseVerifyprojectDepartment pbdv : list){
			DepartmentDTO depart = new DepartmentDTO();
			int PBaseVerifyprojectCandiateId = pbdv.getPBaseVerifyprojectCandiateId();
			int PBaseAccountbookId = pbdv.getPBaseAccountbookId();		
			depart.setDepartment(pbdv);
			if(PBaseVerifyprojectCandiateId != 0){
				PBaseVerifyprojectCandiate manager = candiateDao.getCandiateById(PBaseVerifyprojectCandiateId);
				depart.setManager(manager);
			}
			if(PBaseAccountbookId != 0){
				PBaseAccountbook account = accountDao.getAccountbookById(PBaseAccountbookId);
				depart.setAccountbook(account);
			}
			departments.add(depart);
		}
		return departments;
	}


}
